<!-- 登录和注册组件 -->
<template>
    <div class="max-box">

        <div class="login-reg-box">
            <!-- 标题栏 -->
            <div class="title-line">
                <div v-show="currentStatus == 'normal' || currentStatus == 'phone'"
                    :class="{ 'active': currentStatus == 'normal' }" @click="currentStatus = 'normal'">普通登录</div>
                <div v-show="currentStatus == 'normal' || currentStatus == 'phone'"
                    :class="{ 'active': currentStatus == 'phone' }" @click="currentStatus = 'phone'">手机登录</div>
                <div v-show="currentStatus == 'register'" :class="{ 'active': currentStatus == 'register' }">普通注册</div>
                <div v-show="currentStatus == 'forget'" :class="{ 'active': currentStatus == 'forget' }">忘记密码</div>
            </div>
            <!-- 普通登录 -->
            <div v-show="currentStatus == 'normal'" class="normal-login">
                <div>
                    <input type="text" placeholder="请输入账户名" v-model.trim="username">
                </div>
                <div>
                    <input type="password" placeholder="请输入密码" v-model.trim="password">
                </div>
                <button @click="normalLogin">登录</button>
                <div class="linkToOther">
                    <span @click="currentStatus = 'register'">立即注册！</span>
                    <span @click="currentStatus = 'forget'">忘记密码?</span>
                </div>
            </div>

            <!-- 手机登录 -->
            <div v-show="currentStatus == 'phone'" class="phone-login">
                <div>
                    <input type="text" placeholder="请输入手机号" v-model.trim.number="phoneNumber">
                </div>
                <div>
                    <input type="password" placeholder="请输入验证码" v-model="checkCode">
                    <button @click="btnGetCheckCode(phoneNumber)" ref="checkCodeBtn">获取验证码</button>
                </div>
                <button>登录</button>
                <div class="linkToOther">
                    <span @click="currentStatus = 'register'">立即注册！</span>
                    <span @click="currentStatus = 'forget'">忘记密码?</span>
                </div>
            </div>
            <!-- 普通注册 -->
            <div v-show="currentStatus == 'register'" class="normal-reg">
                <div>
                    <label>
                        <div>账户名</div>
                        <input type="text" placeholder="请输入账户名(6-20位，非纯数字)!" v-model.trim="username">
                    </label>
                </div>
                <div>
                    <label>
                        <div>密码</div>
                        <input type="password" placeholder="请输入你的密码(6-20位，非汉字)!" v-model.trim="password">
                    </label>
                </div>
                <div>
                    <label>
                        <div>确认密码</div>
                        <input type="password" placeholder="请再次输入你的密码!" v-model.trim="confirmPassword">
                    </label>
                </div>
                <div>
                    <label>
                        <div>昵称</div>
                        <input type="text" placeholder="昵称" v-model.trim="nickname">
                    </label>
                </div>
                <div>
                    <label>
                        <div>手机号</div>
                        <input type="number" placeholder="手机号" v-model.trim.number="phoneNumber">
                    </label>
                </div>
                <div id="check-code-box">
                    <label>
                        <div id="check-code-left">验证码</div>
                        <div>
                            <input type="text" placeholder="验证码" v-model.trim="checkCode">
                            <button>获取验证码</button>
                        </div>
                    </label>
                </div>

                <button>注册</button>
                <div class="linkToOther">
                    <span @click="currentStatus = 'normal'">去登录</span>
                </div>
            </div>
            <!-- 忘记密码 -->
            <div v-show="currentStatus == 'forget'" class="forget-password">
                <div>
                    <label>
                        <input type="text" placeholder="请输入账户名" v-model.trim="username">
                    </label>
                </div>
                <div>
                    <label>
                        <input type="password" placeholder="请输入密码" v-model.trim="password">
                    </label>
                </div>
                <div>
                    <label>
                        <input type="password" placeholder="请再次输入密码" v-model.trim="confirmPassword">
                    </label>
                </div>
                <div>
                    <label>
                        <input type="number" placeholder="请输入手机号" v-model.trim.number="phoneNumber">
                    </label>
                </div>
                <div id="code">
                    <input type="password" placeholder="请输入验证码" v-model.trim="checkCode">
                    <button>获取验证码</button>
                </div>
                <button>登录</button>
                <div class="linkToOther" style="flex-direction: row-reverse;">
                    <span @click="currentStatus = 'normal'">去登录</span>
                </div>
            </div>
        </div>
    </div>


</template>

<script>

import { getLoginInfo, getCheckCode,getUserShoppingCartInfo } from '@/api/request'
export default {
    name: 'LoginRegCom',
    data() {
        return {
            currentStatus: 'normal',
            username: '',
            password: '',
            confirmPassword: '',
            checkCode: '',
            phoneNumber: '',
            nickname: '',
            count: 5,
        }
    },
    methods: {
        // 普通登录
        normalLogin() {
            if (!this.username) {
                this.$message.warning('请输入账户名');
                return;
            }
            if (!this.password) {
                this.$message.warning('请输入密码');
                return;
            }
            // 发送登录请求
            getLoginInfo(this.username, this.password).then(res => {
                console.log('getLoginInfo', res, res.code);
                // 登录成功
                if (res.code == 200) {
                    this.$message.success('登录成功！')
                    /* 清除本地数据 */
                    this.username='';
                    this.password='';

                    //关闭登录框
                    this.$store.commit('setShowModel',false)

                    //保存登录状态
                    this.$store.commit('setIsLogin',true)
                    console.log('登录成功 this.$store.state.isLogin', this.$store.state.isLogin);

                    // 保存用户信息
                    this.$store.commit('setUserInfo', res.data)
                    
                    // 获取购物车信息
                    getUserShoppingCartInfo().then(res=>{
                        // 将state中购物车信息和数量初始化
                        this.$store.commit('setShoppingCartList', res.shoppingCartList)
                        this.$store.commit('setCount', res.shoppingCartList.length)
                    }).catch(err=>{
                        console.log(err);
                    });
                }
                else if(res.code==500){
                    this.$message.error('账户名或密码错误');
                }


            })
        },
        btnGetCheckCode(phoneNumber) {
            if (!phoneNumber) {
                this.$message.warning('请输入手机号');
                return;
            }
            var btn = this.$refs.checkCodeBtn
            console.log(btn);
            btn.setAttribute('disabled', 'disabled')
            var timer = setInterval(() => {

                btn.textContent = `${this.count--}秒后重新获取`;
                if (this.count == -1) {
                    clearInterval(timer);
                    this.count = 0;
                    btn.disabled = false;
                    btn.textContent = '重新获取';
                }

            }, 1000)
            getCheckCode(phoneNumber).then(res => {
                console.log('getCheckCode', res);
            }).catch(err => {
                console.log(err);

            })

        }
    }
}
</script>

<style lang="less" scoped>
.active {
    border-bottom: 2px solid rgb(0, 207, 140);
    color: rgb(0, 207, 140);


}

.max-box {
    // width: 100%;
    // height: 100%;
    // background-color: rgba(0, 0, 0, 0.5);
    /* 半透明遮罩 */
    // z-index: 999;

}

.login-reg-box {
    width: 400px;
    padding-bottom: 50px;
    margin: auto;
    background-color: #fff;
    // position: absolute;
    // top: 15%;
    // left: 38%;

    .title-line {
        display: flex;
        justify-content: space-evenly;
        margin-bottom: 20px;

        >div {
            padding: 20px 20px 8px 20px;

            &:hover {
                border-bottom: 2px solid rgb(0, 207, 140);
                color: rgb(0, 207, 140);
            }
        }
    }

    // 输入框的一般样式
    input {
        padding-left: 10px;
        width: 290px;
        height: 32px;
        display: block;
        margin: auto;
        margin-bottom: 25px;
        border: 1px solid #d9d9d9;

        &::placeholder {
            color: #ccc;
        }

        &:focus {
            outline: none;
            border: 1px solid rgb(0, 207, 140);
        }
    }

    // 按钮的一般样式
    button {
        width: 290px;
        height: 32px;
        display: block;
        margin: auto;
        border: none;
        background-color: rgb(0, 207, 140);
        color: #fff;

    }

    .linkToOther {

        width: 280px;
        margin: 0 auto;
        margin-top: 20px !important;
        display: flex;
        justify-content: space-between;
        color: #999;
        font-size: 14px;
        cursor: pointer;

    }


    .phone-login {


        div:nth-of-type(2) {
            display: flex;
            align-items: center;
            width: 290px;
            margin: auto;
            margin-bottom: 20px;

            input {
                width: 160px;
                margin-bottom: 0;
                margin-right: 15px;
            }

            button {
                width: 115px;

            }
        }



    }

    .normal-reg {

        >div {
            width: 360px;
            margin: 0 auto;

            >label {
                display: flex;
                justify-content: center;

                >div {

                    width: 80px;
                    text-align: right;
                    padding-right: 15px;
                }

                input {
                    margin-left: 0;
                    width: 280px;
                }


            }
        }

        #check-code-box {

            margin: 0 auto;

            label {}


            #check-code-left {

                text-align: right;
            }


            div:nth-of-type(2) {
                display: flex;
                width: 280px;
                align-items: flex-start;

                input {
                    width: 150px;
                    margin-right: 20px
                }

                button {
                    width: 110px;
                    margin: 0;
                }
            }
        }

        >div:last-of-type {
            display: flex;
            flex-direction: row-reverse;
        }
    }

    .forget-password {
        >div {
            width: 280px;
            margin: auto;
        }

        #code {
            margin-bottom: 20px;
            display: flex;

            >input {
                width: 150px;
                margin: 0;
                margin-right: 30px;
            }

            button {
                width: 100px;
            }
        }

    }
}
</style>
